package com.siderakis.gwt.scaffold.generated;

import com.axeiya.gwtckeditor.client.CKConfig;
import com.axeiya.gwtckeditor.client.CKEditor;
import com.axeiya.gwtckeditor.client.Toolbar;
import com.axeiya.gwtckeditor.client.ToolbarLine;
import com.axeiya.gwtckeditor.client.CKConfig.TOOLBAR_OPTIONS;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.TextBox;

public class RichEditor extends Composite implements HasText, HasValue<String> {

	final private CKEditor ckeditor;
	final private TextBox txt = new TextBox();

	public RichEditor() {

		CKConfig conf = new CKConfig();

		Toolbar t = getToolbar();
		conf.setToolbar(t);

		conf.setUseFormPanel(false);
		conf.setResizeEnabled(false);
		ckeditor = new CKEditor(conf);
		initWidget(ckeditor);
	}

	private Toolbar getToolbar() {

		ToolbarLine line2 = new ToolbarLine();
		TOOLBAR_OPTIONS[] t2 = { TOOLBAR_OPTIONS.Bold, TOOLBAR_OPTIONS.Italic,
				TOOLBAR_OPTIONS.Underline, TOOLBAR_OPTIONS.Strike,
				TOOLBAR_OPTIONS.Subscript, TOOLBAR_OPTIONS.Superscript,
				TOOLBAR_OPTIONS._, TOOLBAR_OPTIONS.NumberedList,
				TOOLBAR_OPTIONS.BulletedList, TOOLBAR_OPTIONS._,
				TOOLBAR_OPTIONS.Outdent, TOOLBAR_OPTIONS.Indent,
				TOOLBAR_OPTIONS.Blockquote, TOOLBAR_OPTIONS._,
				TOOLBAR_OPTIONS.JustifyLeft, TOOLBAR_OPTIONS.JustifyCenter,
				TOOLBAR_OPTIONS.JustifyRight, TOOLBAR_OPTIONS.JustifyBlock,
				TOOLBAR_OPTIONS._, TOOLBAR_OPTIONS.Image,
				TOOLBAR_OPTIONS.Table, TOOLBAR_OPTIONS.HorizontalRule,
				TOOLBAR_OPTIONS.SpecialChar, TOOLBAR_OPTIONS._,
				TOOLBAR_OPTIONS.Undo, TOOLBAR_OPTIONS.Redo, TOOLBAR_OPTIONS._,
				TOOLBAR_OPTIONS.Source, };
		line2.addAll(t2);

		ToolbarLine line3 = new ToolbarLine();
		TOOLBAR_OPTIONS[] t3 = { TOOLBAR_OPTIONS.Styles,
				TOOLBAR_OPTIONS.Format, TOOLBAR_OPTIONS.Font,
				TOOLBAR_OPTIONS.FontSize, TOOLBAR_OPTIONS._,
				TOOLBAR_OPTIONS.TextColor, TOOLBAR_OPTIONS.BGColor,
				TOOLBAR_OPTIONS._, TOOLBAR_OPTIONS.ShowBlocks,
				TOOLBAR_OPTIONS._, TOOLBAR_OPTIONS.Paste,
				TOOLBAR_OPTIONS.PasteText, TOOLBAR_OPTIONS.PasteFromWord,
				TOOLBAR_OPTIONS._, TOOLBAR_OPTIONS.Find,
				TOOLBAR_OPTIONS.Replace, };
		line3.addAll(t3);

		Toolbar t = new Toolbar();
		t.addSeparator();
		t.add(line2);
		t.addSeparator();
		t.add(line3);

		return t;
	}

	@Override
	public String getValue() {
		return getText();
	}

	@Override
	public void setValue(String value) {
		setText(value);
	}

	public String getText() {
		return this.ckeditor.getData();

	}

	public void setText(String text) {
		this.ckeditor.setData(text);
	}

	@Override
	public void setValue(String value, boolean fireEvents) {
		setText(value);
	}

	@Override
	public HandlerRegistration addValueChangeHandler(
			ValueChangeHandler<String> handler) {
		GWT.log("add value changed.");
		return txt.addValueChangeHandler(handler);
	}

	public void saveClicked() {
		txt.setValue(ckeditor.getData(), true);
	}

}
